import json
import re
from utils import get_book_url, books_contain, book_equal, get_book_domain


def remove_duplicate(book_list: list):
    remove_num = 0
    dst_list = []
    pre_url = ''
    pre_books = []
    for book in book_list:
        cur_url = get_book_url(book)
        if cur_url == pre_url:
            if books_contain(pre_books, book):
                remove_num += 1
                continue
        else:
            pre_url = cur_url
            pre_books = []
        dst_list.append(book)
        pre_books.append(book)
    print("Remove {} duplicate books".format(remove_num))
    return dst_list


def sort_books(book_list: list):
    book_list = sorted(book_list, key=get_book_domain)
    book_list = remove_duplicate(book_list)
    return book_list


def main():
    src_path = './output/url_ok.json'
    dst_path = './output/url_sort.json'

    with open(src_path, 'r', encoding='utf-8') as f:
        src_books = json.load(f)

    dst_books = sort_books(src_books)

    with open(dst_path, 'w', encoding='utf-8') as f:
        json_str = json.dumps(dst_books)
        f.write(json_str)


if __name__ == '__main__':
    main()
